<script setup>
	import {
		onShow
	} from "@dcloudio/uni-app";
	import {
		reactive,
	} from 'vue';

	const user = reactive({
		nick: '请点击登录',
		token: '',
	})

	function goLogin() {
		uni.navigateTo({
			url: '/pages/user/login'
		})
	}
	function exitLogin() {
		uni.showModal({
			title: '提醒',
			content: '确认要退出登录吗？',
			success(res) {
				if(res.confirm) {
					uni.removeStorageSync('token')
					goLogin()					
				}
			}
		})
	}

	onShow(() => {
		const tokenData = uni.getStorageSync('token')
		console.log(tokenData);
		if (tokenData) {
			user.nick = '已登录'
			user.token = JSON.stringify(tokenData, null , 4)
		} else {
			user.nick = '请点击登录'
			user.token = ''
		}
	})
</script>

<template>
	<view class="zg__page">
		<view class="zg__userIcon">
			<image class="zg__userIcon" src="../../static/logo.png" mode=""></image>
		</view>
		<view class="zg__userNick">
			<text>{{ user.nick }}</text>
		</view>
		<template v-if="user.token">			
			<view class="zg__userLogin">
				<button type="warn" @click="exitLogin">退出</button>
			</view>
			<view class="zg__userLogin">
				{{ user.token }}
			</view>
		</template>
		<view class="zg__userLogin" v-else>
			<button type="primary" @click="goLogin">登录</button>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	.zg__page {
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.zg__userIcon {
		image {
			width: 200rpx;
			height: 200rpx;
			border-radius: 50%;
		}
	}

	.zg__userNick {
		margin-top: 10rpx;
		word-break: break-all;

		text {
			font-size: 42rpx;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			width: 400rpx;
			color: $uni-text-color-grey;
		}
	}

	.zg__userLogin {
		margin-top: 30rpx;

		button {
			width: 300rpx;
			height: 100rpx;
			border-radius: 50rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>